var file_upload = {
	btnUpload : {},
	status : {},
	options: {
		action: '/uploada.php',//Куда будет слаться файл
		name: 'p',
		btnUpload: '#image',
		allowedExtensions:['jpeg', 'jpg', 'png'],
	},
	init: function(opt) {
		file_upload.options = $.extend(file_upload.options , opt);
		file_upload.btnUpload = $(file_upload.options.btnUpload);
		file_upload.status = $(file_upload.options.status);
		_this = this;
		$(function() {
			var options = file_upload.options;
			new AjaxUpload(file_upload.btnUpload, {
				action: options.action,
				name: options.name,
				onSubmit: function(file, ext) {
					if (!(ext && /^(?:jpg|jpeg|png)$/i.test(ext))) {
						file_upload.status.text('');
						return false;
					}
					CORE.ShowLoading();
				},
				onComplete: function (file, response) {
					file_upload.btnUpload.value = response['name'];
					var response = jQuery.parseJSON(response);
					$('#image').val(response.msg2);
					var prev = $("#preview");
					// if (prev.attr('src') != 'images/spacer.png'){
						// var xhr = new XMLHttpRequest();
						// xhr.open('GET', '/uploada.php?del=1&filename=' + prev.attr('src'), true);
						// xhr.onreadystatechange = function() {
							// if (xhr.readyState != 4) return;
							// if (xhr.status != 200) {
								// alert('Ошибка ' + xhr.status + ': ' + xhr.statusText);
								// return;
							// }
						// }
						// xhr.send(null);
					// }
					CORE.HideLoading();
					$("#preview").prop("src",response.msg2).closest("tr").show();
				}
			});
		});
	}
};

$(function() {
	if ($(file_upload.options.btnUpload).length) 
		file_upload.init();
});